package com.yy.yyui.popup
{
	import com.util.tools.GT;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 弹出窗口类
	 * @author YY
	 * 
	 */
	public class PopUpWindow extends Sprite
	{
		private var source:DisplayObject;
		private var dragSprite:Sprite;
		private var target:DisplayObjectContainer;
		public var blocker:Sprite;
		
		private var modal:Boolean;
		public function PopUpWindow(source:DisplayObject,target:DisplayObjectContainer,modal:Boolean=true,modalEffect:Boolean=true,dragState:Boolean = true)
		{
			super();
			var dobj:DisplayObject;
			this.source = source;
			this.target = target;
			
			//是否是模态窗口
			if(modal){
				blocker = new Sprite();
				var rect:Rectangle = target.getBounds(target.parent);
				var tHeight:Number = target.height;
//				GT.fulFill3(blocker,target.width,tHeight,0xffffff,modalEffect?0:0);
				blocker.addChild(GT.getRectGraphic(target.width,tHeight,0x000000, 0x000000, 0,.1, .1));
				this.addChild(blocker);
			}
			//判断是否是单例对话框
			if(source.hasOwnProperty("instance"))
			{
				if((source as Object).instance)
				{
					(source as Object).blocker = blocker;
				}
			}
			
			dragSprite = new Sprite();
			this.addChild(source);
			source.x = (target.width - source.width)/2;
			source.y = (target.height - source.height)/2;
			if(source.y<0) source.y = 0;
			if(source.x<0) source.x = 0;
			if(dragState)
			{
				this.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
			}
			this.addChild(dragSprite);
			
			if(modal){
				//target.addEventListener(Event.ENTER_FRAME, onResize);
				oldTargetWidth = target.width;
				oldTargetHeight = target.height;	
			}
			this.modal = modal;
			source.addEventListener(Event.REMOVED_FROM_STAGE,close);
		}
		
		private var oldTargetWidth:Number;
		private var oldTargetHeight:Number;
		private function onResize(e:Event):void{
			var newWidth:Number = target.width;
			var newHeight:Number = target.height;
			if(newWidth!=oldTargetWidth||newHeight!=oldTargetHeight){
				blocker.width = newWidth;
				blocker.height = newHeight;
				oldTargetWidth = newWidth;
				oldTargetHeight = newHeight;
			}
		} 
		
		private function close(e:Event):void{
			if(e.target == source){
				source.removeEventListener(Event.REMOVED_FROM_STAGE,close);
				this.removeEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragging);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);				
//				this.target.removeEventListener(Event.ENTER_FRAME, onResize);		
			}
		}


		private var oldX:Number,oldY:Number;
		private function beginDrag(e:MouseEvent):void{
			var sx:Number = e.stageX;
			var sy:Number = e.stageY;
			var x:Number = sx-target.x-this.x;
			var y:Number = sy-target.y-this.y;
			
			x -= source.x;
			y -= source.y;
			if((x>30&&x<source.width-30&&y>-10&&y<30)
			||(y<0&&(source is DisplayObjectContainer)&&(source as DisplayObjectContainer).contains(e.target as DisplayObject))){
				this.stage.addEventListener(MouseEvent.MOUSE_MOVE,dragging);
				this.stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
				this.addEventListener(MouseEvent.ROLL_OUT,endDrag);
				oldX =e.stageX;
				oldY =e.stageY;
			}
		}
		
		private function dragging(e:MouseEvent):void{
			var sx:Number = e.stageX;
			var sy:Number = e.stageY;
			if(sx>source.stage.stageWidth||sx<0||sy>source.stage.stageHeight||sy<0){
				return;
			}

			var x:Number,y:Number;
			x = sx;
			y = sy;
			source.x += (x-oldX);
			source.y += (y-oldY);						
			
			var rect:Rectangle;
			rect = this.source.getBounds(this.target);
			
			var re:Number;
			var be:Number;
			
			if(this.target is Stage){
				re = this.target.stage.stageWidth;
				be = this.target.stage.stageHeight;
			}else{
				re = this.target.width;
				be = this.target.height;
			}
			
			if(source.width<=re){
				if(rect.right>re){
					source.x -= rect.right-re; 
				}
				if(rect.x<0){
					source.x += -rect.x;
				}				
			}else{
				if(rect.right<re){
					source.x += re-rect.right; 
				}
				if(rect.x>0){
					source.x -= rect.x;
				}
			}
			if(source.height<=be){
				if(rect.bottom>be){
					source.y -= rect.bottom-be;
				}
				if(rect.y<0){
					source.y += -rect.y;
				}				
			}else{
				if(rect.bottom<be){
					source.y += be-rect.bottom;
				}
				if(rect.y>0){
					source.y -= rect.y;
				}
			}
			
			oldX = x;
			oldY = y;
			e.updateAfterEvent();
		}
		
		/**
		 *停止拖动 
		 * @param e
		 * 
		 */		
		private function endDrag(e:MouseEvent):void{
			if(this.stage!=null){
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,dragging);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
			}
			this.removeEventListener(MouseEvent.ROLL_OUT,endDrag);
		}
	}
}